package com.woniuxy.food.app.config;

import exception.MallException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import result.CommonResultCodeEnum;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class StoreInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //如果不是网关来的服务，都拒绝访问
        System.out.println("进入拦截器");
        String fromKey = request.getHeader("systemFrom");
        System.out.println(fromKey);
        if(fromKey == null||fromKey.equals("")){
            throw new MallException("拒绝访问", CommonResultCodeEnum.NOT_AUTHORIZED);
        }
        if (!fromKey.equals("store")) {
            throw new MallException("拒绝访问", CommonResultCodeEnum.NOT_AUTHORIZED);
        }
        return true;
    }
}
